package org.niugang.security.common.mq;


import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.niugang.security.log.pojo.LogDO;
import org.niugang.security.log.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * 从redis获取消息，保存数据库，异步操作
 *
 * @author niugang
 */
@Slf4j
public class MessageDelegateImpl implements MessageDelegate {


    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private LogService logService;

    @Override
    public void handleMessage(String message) {
        log.info("Operate log [{}]", message);
        LogDO logEntity = null;
        try {
            logEntity = objectMapper.readValue(message, LogDO.class);
            logService.save(logEntity);
            log.info("logEntity [{}]", logEntity != null ? logEntity : "");
        } catch (IOException e) {
            log.info("Parse josn error [{}]", e.getMessage());
        }


    }

}
